/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.chooser.controller;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import offset.nodes.client.chooser.view.LazyExpandingNode;
import offset.nodes.client.chooser.view.NodeChooserDialog;
import offset.nodes.client.chooser.model.NewFolder;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.ServerResponse;
import offset.nodes.client.view.NodeNameDialog;

/**
 *
 * @author Walter Lütgenau
 */
public class NodeChooserActions {
    public static abstract class AbstractChooserAction extends AbstractAction {
        public AbstractChooserAction(String name) {
            super(name);
        }
        
        public NodeChooserDialog getDialog(ActionEvent e) {
            return (NodeChooserDialog) ((JComponent)e.getSource()).getRootPane().getParent();
        }
        
      }
    
    public static class NewFolderAction extends AbstractChooserAction {
        Server server;

        public NewFolderAction(Server server) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/view/resources/ClientBundle").getString("chooser.newFolder"));
            this.server = server;
        }
        
        String pathToString(TreePath path) {
            StringBuffer buf = new StringBuffer();
            for( int i=0; i<path.getPathCount(); i++) {
                if ( i > 0)
                    buf.append("/");
                buf.append(path.getPathComponent(i).toString());
            }
            
            return buf.toString();
        }

        public void actionPerformed(ActionEvent e) {
            JTree tree = getDialog(e).getNodeTree();
            TreePath path = tree.getSelectionPath();
            LazyExpandingNode node = (LazyExpandingNode) path.getLastPathComponent();
            if ( !node.getAllowsChildren())
                node = (LazyExpandingNode) node.getParent();
            
            NodeNameDialog nodeNameDialog = new NodeNameDialog(null, true);
            nodeNameDialog.setVisible(true);
            if ( nodeNameDialog.getReturnStatus() == NodeNameDialog.RET_OK) {
                NewFolder.Request request = new NewFolder.Request();
                request.setName(nodeNameDialog.getName());
                request.setPath(pathToString(path));
                try {
                    NewFolder.Response response = (NewFolder.Response) server.sendRequest(request);
                    if ( response.getResult() != ServerResponse.RESULT_SUCCESS)
                        return;
                    node.repopulateFolders();
                    ((DefaultTreeModel)tree.getModel()).reload(node);
                    for( int i=0; i<node.getChildCount(); i++) {
                        LazyExpandingNode child = (LazyExpandingNode) node.getChildAt(i);
                        if ( child.getName().equals(nodeNameDialog.getName())) {
                            tree.setSelectionPath(new TreePath(child.getPath()));
                            return;
                        }
                    }
                }
                catch( Exception ex) {
                    ex.printStackTrace();
                    return;
                }
             }
        }
        
    }
    
}
